package st.business.tepth.business.businfo;

import com.alibaba.fastjson.TypeReference;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.BusDriverInfoBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-27 10:33
 */

public class BusInfoModel implements BusInfoContract.Model {

    @Override
    public Disposable getBusInfo(String token, int repairId,
                                 final OnDataConvertListener<BusDriverInfoBean> listener) {
        return RxRestClient.builder()
                .url(Url.GetInfoForBusDriver)
                .params("token", token)
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToInfo(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    private BusDriverInfoBean convertToInfo(String response) {
        return new ObjConverter<BusDriverInfoBean>().convert(response
                , new TypeReference<BusDriverInfoBean>() {
                });
    }
}
